package com.agileboot.admin.controller.exam;

import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import com.agileboot.domain.answer.vo.AnswerVO;
import com.agileboot.domain.bank.db.QuestionBank;
import com.agileboot.domain.bank.db.QuestionBankService;
import com.agileboot.domain.bank.query.ExamQuery;
import com.agileboot.domain.bank.query.ExamRequest;
import com.agileboot.domain.bank.vo.BankHaveQuestionSum;
import com.agileboot.domain.exam.db.Exam;
import com.agileboot.domain.exam.db.ExamService;
import com.agileboot.domain.exam.form.ExamForm;
import com.agileboot.domain.exam.form.PaperData;
import com.agileboot.domain.exam.query.ExamPaperQuery;
import com.agileboot.domain.exam.vo.AddExamByBankVo;
import com.agileboot.domain.exam.vo.AddExamByQuestionVo;
import com.agileboot.domain.exam.vo.ExamQueryVo;
import com.agileboot.domain.examquestion.db.ExamQuestion;
import com.agileboot.domain.examquestion.db.ExamQuestionService;
import com.agileboot.domain.examrecord.db.ExamRecord;
import com.agileboot.domain.examrecord.db.ExamRecordService;
import com.agileboot.domain.examrecord.query.ExamRecordQuery;
import com.agileboot.domain.examrecord.vo.ExamRecordInfoVO;
import com.agileboot.domain.examrecord.vo.ExamRecordVO;
import com.agileboot.domain.medicine.dto.MedicineDTO;
import com.agileboot.domain.question.db.Question;
import com.agileboot.domain.question.db.QuestionService;
import com.agileboot.domain.question.form.QuestionForm;
import com.agileboot.domain.question.query.QuestionQuery;
import com.agileboot.domain.question.vo.QuestionAnswerVO;
import com.agileboot.domain.question.vo.QuestionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@RestController
@RequiredArgsConstructor
@Api(tags = "三个角色公共的相关接口")
@RequestMapping(value = "/public")
public class PublicController {

    private final ExamService examService;

    private final ExamRecordService examRecordService;

    private final ExamQuestionService examQuestionService;

    private final QuestionService questionService;

    private final QuestionBankService questionBankService;

    /**
     * =============================================题库==============================================
     */

    @GetMapping("/getQuestionBankPage")
    @ApiOperation("获取题库分页列表数据")
    public ResponseDTO<PageDTO<BankHaveQuestionSum>> getQuestionBankPage(ExamQuery examQuery) {
        return ResponseDTO.ok(questionBankService.getQuestionBankPage(examQuery));
    }

    @PostMapping("/addQuestionBank")
    @ApiOperation("添加题库")
    public ResponseDTO addQuestionBank(@RequestBody ExamRequest examQuery) {
        questionBankService.addQuestionBank(examQuery);
        return ResponseDTO.ok("添加成功");
    }

    @Operation(summary = "删除题库信息")
//    @PreAuthorize("@permission.has('system:user:list')")
    @DeleteMapping("/{ids}")
    public ResponseDTO<Void> deleteQuestionBank(@PathVariable("ids") String ids) {
        questionBankService.deleteQuestionBank(ids);
        return ResponseDTO.ok();
    }

    @Operation(summary = "修改题库详情")
    @PutMapping("/{id}")
    public ResponseDTO<Void> editQuestionBank(@PathVariable("id") Integer id, @RequestBody ExamRequest examQuery) throws Exception {
        questionBankService.editQuestionBank(id, examQuery);
        return ResponseDTO.ok();
    }


    /**
     * ============================================试题===============================================
     */

    @GetMapping("/question/list")
    @ApiOperation("试题分页列表")
    public ResponseDTO<PageDTO<Question>> getQuestion(QuestionQuery query) {
        return ResponseDTO.ok(questionService.getQuestion(query));
    }

    @PostMapping("/question/add")
    @ApiOperation("添加试题")
    public ResponseDTO addQuestion(@RequestBody @Valid QuestionForm questionForm) {
        questionService.addQuestion(questionForm);
        return ResponseDTO.ok("新增成功");
    }

    @ApiOperation("根据id批量删除")
    @DeleteMapping("/question/{ids}")
    public ResponseDTO deleteQuestion(@PathVariable("ids") String ids) {
        questionService.deleteQuestionByIds(ids);
        return ResponseDTO.ok("新增成功");
    }


    @PostMapping("/question/edit/{id}")
    @ApiOperation("更新试题")
    public ResponseDTO updateQuestion(@PathVariable("id") Integer id,@RequestBody QuestionForm questionForm) {
        questionService.updateQuestion(id,questionForm);
        return ResponseDTO.ok("修改成功");
    }

    @GetMapping("/getAllQuestionBankName")
    @ApiOperation("得到所有题库名称列表")
    public ResponseDTO<List<QuestionBank>> getAllQuestionBankName() {
        return ResponseDTO.ok(questionBankService.getAllQuestionBankName());
    }

    @GetMapping("/getQuestionDetail/{id}")
    @ApiOperation("得到题目的详情")
    public ResponseDTO<QuestionAnswerVO> getQuestionDetail(@PathVariable("id") Integer id) {
        return ResponseDTO.ok(questionBankService.getQuestionDetail(id));
    }




    /**
     * ============================================考试===============================================
     */
    @GetMapping("/paper/list")
    @ApiOperation("试卷分页列表")
    public ResponseDTO<PageDTO<ExamForm>> getExam(ExamPaperQuery query) {
        return ResponseDTO.ok(examService.getExam(query));
    }

    @GetMapping("/paper/data/{id}")
    @ApiOperation("得到PaperData")
    public ResponseDTO<List<PaperData>> getPaperData(@PathVariable("id") Integer id) {
        return ResponseDTO.ok(examService.getPaperData(id));
    }

    @PostMapping("/paper/add")
    @ApiOperation("添加试题")
    public ResponseDTO addExam(@RequestBody ExamForm examForm) {
        examService.addExam(examForm);
        return ResponseDTO.ok("新增成功");
    }

    @ApiOperation("根据id批量删除")
    @DeleteMapping("/paper/{ids}")
    public ResponseDTO deleteExam(@PathVariable("ids") String ids) {
        examService.deleteExam(ids);
        return ResponseDTO.ok("新增成功");
    }


    @PostMapping("/paper/edit/{id}")
    @ApiOperation("更新试题")
    public ResponseDTO updateExam(@PathVariable("id") Integer id,@RequestBody ExamForm examForm) {
        examService.updateExam(id,examForm);
        return ResponseDTO.ok("修改成功");
    }



    /**
     * ============================================阅卷===============================================
     */

    @GetMapping("/record/list")
    @ApiOperation("阅卷分页列表")
    public ResponseDTO<PageDTO<ExamRecordVO>> getRecordExam(ExamRecordQuery query) {
        return ResponseDTO.ok(examRecordService.getRecordExam(query));
    }

    @GetMapping("/record/info")
    @ApiOperation("试卷详情")
    public ResponseDTO<List<ExamRecordInfoVO>> getRecordExamDetail(Integer id) {
        return ResponseDTO.ok(examRecordService.getRecordExamDetail(id));
    }

    @GetMapping("/getExamInfoById")
    @ApiOperation("根据考试id查询考试的信息和题目列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examId", value = "考试id", required = true, dataType = "int", paramType = "query")
    })
    public ResponseDTO<AddExamByQuestionVo> getExamInfoById(@RequestParam Integer examId) {
        return  ResponseDTO.ok(examService.getExamInfoById(examId));
    }

    @GetMapping("/getExamQuestionByExamId/{examId}")
    @ApiOperation("根据考试id查询考试中的每一道题目id和分值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examId", value = "考试id", required = true, dataType = "int", paramType = "query")
    })
    public ResponseDTO<ExamQuestion> getExamQuestionByExamId(@PathVariable Integer examId) {
        return ResponseDTO.ok(examQuestionService.getExamQuestionByExamId(examId));
    }



    @GetMapping("/getQuestionByIds")
    @ApiOperation("根据id集合获取题目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "问题id集合", required = true, dataType = "int", paramType = "params")
    })
    public ResponseDTO<PageDTO<QuestionVo>> getQuestionById(@RequestParam("ids") List<Integer> ids) {
        return  ResponseDTO.ok(questionService.getQuestionVoByIds(ids));
    }

    /**
     * ============================================其他===============================================
     */

    @PostMapping("/getExamInfo")
    @ApiOperation("根据信息查询考试的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "examQueryVo", value = "考试信息查询vo对象", required = true, dataType = "examQueryVo", paramType = "body")
    })
    public PageDTO<Exam> getExamInfo(@RequestBody ExamQueryVo examQueryVo) {
        return examService.getExamPage(examQueryVo);
    }


    @GetMapping("/allExamInfo")
    @ApiOperation("查询考试所有信息")
    public List<Exam> allExamInfo() {
        return examService.list(null);
    }


    @GetMapping("/getQuestionByBankIdAndType")
    @ApiOperation("根据题库id和题目类型获取题目信息 type(1单选 2多选 3判断)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankId", value = "题库id", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "题目类型", required = true, dataType = "int", paramType = "query"),
    })
    public List<QuestionVo> getQuestionByBankIdAndType(Integer bankId, Integer type) {
        return questionBankService.getQuestionByBankIdAndType(bankId, type);
    }

    @GetMapping("/getQuestionByBank")
    @ApiOperation("根据题库获取所有的题目信息(单选,多选,判断题)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankId", value = "题库id", required = true, dataType = "int", paramType = "query")
    })
    public List<QuestionVo> getQuestionByBank(Integer bankId) {
        return questionBankService.getQuestionsByBankId(bankId);
    }

    @GetMapping("/getExamRecordById/{recordId}")
    @ApiOperation("根据考试的记录id查询用户考试的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "考试记录id", required = true, dataType = "int", paramType = "query")
    })
    public ResponseDTO<ExamRecord> getExamRecordById(@PathVariable Integer recordId) {
        return ResponseDTO.ok(examService.getExamDetailInfoById(recordId));
    }

}
